package com.xcn.jdbc;


import java.sql.*;

import org.apache.log4j.*;

/**
 * DB Connection
 * 
 * @since 2011-10-13
 * @author jochangmin
 *
 */
public class DBConnection
{
	private static Logger LOGGER = Logger.getLogger ( Logger.class );
	
	private static String driver = null;
	
	private static String url = null;
	
	private static String database = null;
	
	private static String userid = null;
	
	private static String userpw = null;

	public DBConnection( String driver, String url, String database, String userid, String userpw ) throws Exception
	{
		this.driver = driver;
		this.url = url;
		this.database = database;
		this.userid = userid;
		this.userpw = userpw;
	}
	
	/**
	 * getConnection
	 * Connection
	 * 
	 * @return Connection
	 * @throws Exception 
	 */
	public Connection getConnection ( ) throws Exception
	{
		Connection conn = null;
		try
		{
			Class.forName ( driver ).newInstance ( );
			if ( driver.toLowerCase ( ).indexOf ( "oracle" ) > -1 )
				conn = DriverManager.getConnection ( url + ":" + database, userid, userpw );
			else
				conn = DriverManager.getConnection ( url + "/" + database, userid, userpw );
		}
		catch ( SQLException e )
		{
			LOGGER.error ( "Connection SQLException : " + e.getMessage ( ) );
			throw new Exception ( e.getMessage ( ) );
		}
		catch ( Exception e )
		{
			LOGGER.error ( "Connection Exception : " + e.getMessage ( ) );
			e.printStackTrace ( );
			throw new Exception ( e.getMessage ( ) );
		}
		return conn;
	}
	
	/**
	 * getConnection
	 * Connection
	 * 
	 * @return Connection
	 * @throws Exception 
	 */
	public static Connection getConnection ( String driver, String url, String database, String userid, String userpw ) throws Exception
	{
		Connection conn = null;
		try
		{
			Class.forName ( driver ).newInstance ( );
			if ( driver.toLowerCase ( ).indexOf ( "oracle" ) > -1 )
				conn = DriverManager.getConnection ( url + ":" + database, userid, userpw );
			else
				conn = DriverManager.getConnection ( url + "/" + database, userid, userpw );
		}
		catch ( SQLException e )
		{
			LOGGER.error ( "Connection SQLException : " + e.getMessage ( ) );
			throw new Exception ( e.getMessage ( ) );
		}
		catch ( Exception e )
		{
			LOGGER.error ( "Connection Exception : " + e.getMessage ( ) );
			e.printStackTrace ( );
			throw new Exception ( e.getMessage ( ) );
		}
		return conn;
	}
	
	/**
	 * getConnection
	 * Connection
	 * 
	 * @return Connection
	 * @throws Exception 
	 */
	public Connection getConnectionRoot ( ) throws Exception
	{
		Connection conn = null;
		try
		{
			Class.forName ( driver ).newInstance ( );
			conn = DriverManager.getConnection ( url, userid, userpw );
		}
		catch ( SQLException e )
		{
			LOGGER.error ( "Connection SQLException : " + e.getMessage ( ) );
			e.printStackTrace ( );
			throw new Exception ( e.getMessage ( ) );
		}
		catch ( Exception e )
		{
			LOGGER.error ( "Connection Exception : " + e.getMessage ( ) );
			e.printStackTrace ( );
			throw new Exception ( e.getMessage ( ) );
		}
		return conn;
	}
	
	/**
	 * Connection Close
	 * 
	 * @param conn
	 */
	public static void close ( Connection conn ) throws Exception
	{
		close ( conn, null, null, null );
	}
	
	/**
	 * Connection Close
	 * 
	 * @param conn
	 * @throws Exception 
	 */
	public static void close ( Connection conn, ResultSet rs, PreparedStatement ps, Statement st ) throws Exception
	{
		if ( st != null )
		{
			try
			{
				st.close ( );
			}
			catch ( SQLException e )
			{
				LOGGER.error ( "Mysql Statement close Exception : " + e.getMessage ( ) );
				e.printStackTrace ( );
			}
		}

		if ( ps != null )
		{
			try
			{
				ps.close ( );
			}
			catch ( SQLException e )
			{
				LOGGER.error ( "Mysql PreparedStatement close Exception : " + e.getMessage ( ) );
				e.printStackTrace ( );
			}
		}

		if ( rs != null )
		{
			try
			{
				rs.close ( );
			}
			catch ( SQLException e )
			{
				LOGGER.error ( "Mysql ResultSet close Exception : " + e.getMessage ( ) );
				e.printStackTrace ( );
			}
		}
		
		if ( conn != null )
		{
			try
			{
				conn.close ( );
			}
			catch ( SQLException e )
			{
				LOGGER.error ( "Mysql Connection close Exception : " + e.getMessage ( ) );
				e.printStackTrace ( );
			}
		}
	}
}
